package jun20_13;

/**
 * This class represents a single rectangle. The rectangle is represented by: the (x,y) coordinates of its lower left corner and by its width and height (both integers)
 * @author Amit Shesh
 *
 */

public class Rectangle
{
	/**
	 * instance variables needed to represent one rectangle
	 */
	private int x,y,width,height;
	String name;
	
	/**
	 * Constructor: sets the instance variables to their appropriate values
	 * @param name
	 * 			A name of this rectangle 
	 * @param x
	 * 			The x-coordinate of the lower left corner of this rectangle
	 * @param y
	 * 			The y-coordinate of the lower left corner of this rectangle
	 * @param width
	 * 			The width of this rectangle
	 * @param height
	 * 			The height of this rectangle
	 */
	
	public Rectangle(String name,int x,int y,int width, int height)
	{
		this.name = name;
		this.x = x;
		this.y = y;
		if (width>0)
			this.width = width;
		else
			width = 0;
			
		if (height>0)
			this.height = height;
		else
			height = 0;
	}
	
	/**
	 * Print the contents of this rectangle to screen
	 */
	
	public void print()
	{
		System.out.println(name+":"+"x="+x+", y="+y+", width="+width+", height="+height);
	}
	
	/**
	 * Get the x-coordinate of the lower-left corner of this rectangle object
	 * @return The x-coordinate of the lower left corner of this rectangle object
	 */
	
	public int getX()
	{
		return x;
	}
	
	/**
	 * Get the y-coordinate of the lower-left corner of this rectangle object
	 * @return The y-coordinate of the lower left corner of this rectangle object
	 */
	
	public int getY()
	{
		return y;
	}
	
	/**
	 * Get the width of this rectangle object
	 * @return The width of this rectangle object
	 */
	
	public int getWidth()
	{
		return width;
	}
	
	/**
	 * Get the height of this rectangle object
	 * @return The height of this rectangle object
	 */
	
	public int getHeight()
	{
		return height;
	}
	
	/**
	 * Sets the lower left corner of this rectangle
	 * @param x The new x-coordinate of the lower left corner of this rectangle
	 * @param y The new y-coordinate of the lower left corner of this rectangle
	 */
	
	public void setLowerLeftCorner(int x,int y)
	{
		this.x = x;
		this.y = y;
	}
	
	
	
	/**
	 * Calculates and returns the area of this rectangle, defined as the product of its width and height
	 * @return the area of this rectangle
	 */
	
	public int area()
	{
		return width*height;
	}
	
	/**
	 * Compares two rectangles to see if they overlap each other.
	 * @param a Rectangle object that must be compared to this rectangle object
	 * @return 
	 * 			true if the two rectangles overlap, false otherwise
	 */
	
	public boolean overlaps(Rectangle a)
	{
		if (((a.x>=x) && (a.x<=(x+width))) || ((x>=a.x) && (x<=(a.x+a.width))))
		{
			if (((a.y>=y) && (a.y<=(y+height))) || ((y>=a.y) && (y<=(a.y+a.height))))
			{
				return true;
			}
		}
		
		return false;
	}
	
	/**
	 * Computes and returns the rectangle formed by the intersection of two rectangles (i.e. the rectangle enclosing the common area between two rectangles)
	 * @param a The rectangle object whose intersection with this rectangle is to be calculated
	 * @return
	 * 			A Rectangle object that represents the intersection of the two rectangles. If they do not intersect, this method returns a rectangle with zero width and height
	 */
	
/*	public Rectangle intersect(Rectangle a)
	{
		int left,top,right,bottom;
		
		//the intersect is defined by the rectangle whose top left corner is the "maximum" of the two corners, and whose bottom right corner is the "minimum" of the two bottom right corners
		
		//finding the rightmost between the two start points
		if (x>a.x)
			left = x;
		else
			left = a.x;
			
		//finding the bottommost between the two start points
			
		if (y>a.y)
			top = y;
		else
			top = a.y;
			
		//finding the leftmost between the two bottom right points
			
		if ((x+width)<(a.x+a.width))
			right = x+width;
		else
			right = a.x+a.width;
			
		//finding the uppermost between the two bottom right points
		
		if ((y+height)<(a.y+a.height))
			bottom = y+height;
		else
			bottom = a.y+a.height;
			
		//System.out.println("left="+left+" right="+right+" top="+top+" bottom="+bottom);
		return new Rectangle(this.name+"-intersection-"+a.name,left,top,right-left,bottom-top);
	}	*/
}
	